package com.weishu.installment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.weishu.installment.domain.*;
import com.weishu.installment.domain.shoufang.SearchSfLoanDetailRequestDto;
import com.weishu.installment.domain.shoufang.SearchSfLoanDetailsResponseDto;
import com.weishu.installment.domain.shoufang.SfContractDetailsResponseDTO;
import com.weishu.installment.domain.shoufang.SfLoanDetailRequestBean;
import com.weishu.installment.service.SfContractEntityService;
import com.weishu.installment.service.TJrSfLoanService;
import com.weishu.installment.utils.BeanUtils;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("shoufang")
public class SfController {
    @Resource
    SfContractEntityService service;


    @PostMapping("list")
    @RequiresPermissions("shoufang:shoufang:list")
    public Map list(@RequestBody SfContractEntity ment){
        System.err.println(ment.getPageNum());
        System.err.println(ment.getPageSize());
        Page page = new Page(ment.getPageNum(),ment.getPageSize());
        QueryWrapper<SfContractEntity> wrapper = new QueryWrapper<>();
        if (ment.getOwnerName()!=null&&!ment.getOwnerName().equals("")){
            wrapper.like("owner_name",ment.getOwnerName());
        }
        if (ment.getPremiseName() !=null&&!ment.getPremiseName().equals("")){
            wrapper.like("premise_name",ment.getPremiseName());
        }
        Page page1 = service.page(page,wrapper);
        System.out.println(page1.getRecords());
        Map map = new HashMap();
        map.put("total",page1.getTotal());
        map.put("list",page1.getRecords());
        return map;
    }

    @Resource
    TJrSfLoanService jrSfLoanService;

    @PostMapping("save")
    @RequiresPermissions("shoufang:shoufang:save")
    public Map save(@RequestBody TJrSfLoan ment){
        Map map = new HashMap();
        ment.setCreateTime(new Date());
        boolean save = jrSfLoanService.save(ment);
        if (save){
            map.put("code",200);
            return map;
        }
        map.put("code",202);
        return map;
    }


    @RequiresPermissions("shoufang:shoufang:listSfLoan")
    @GetMapping("listSfLoan")
    public Map listSfLoan(SfLoanDetailRequestBean bean){
        System.err.println(bean);
        // 收房贷计算
        SfContractDetailsResponseDTO sfentity = service.getSfContractStagingCalculation(bean.getContractId(),bean.getProductId());
        SearchSfLoanDetailRequestDto sfdto = new SearchSfLoanDetailRequestDto();
        BeanUtils.copy(bean,sfdto);
        sfdto.setBalance(Double.parseDouble(bean.getBalance()));	// 借款金额
        sfdto.setSxBalance(Double.parseDouble(bean.getSxBalance())); // 授信额
        sfdto.setPeriods(bean.getPeriods());	// 期数
        sfdto.setStartDate(bean.getStartDate()); // 合同开始日
        sfdto.setEndDate(bean.getEndDate());	// 合同结束日
        // 分期计算
        SearchSfLoanDetailsResponseDto sfr= jrSfLoanService.getSfLoanDetail(sfdto);
        Map map = new HashMap();
        map.put("dataList",sfr);
        System.out.println(sfr);
        map.put("sfentity",sfentity);
        return map;
    }





}
